package org.bangumibuddy.parser.impl;

import com.sun.syndication.feed.synd.SyndEnclosure;
import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.bangumibuddy.parser.AnimeMetaParser;
import org.bangumibuddy.parser.RSSParser;
import org.bangumibuddy.parser.TorrentParser;
import org.bangumibuddy.parser.factory.AnimeMetaParserFactory;
import org.bangumibuddy.pojo.TorrentFile;
import org.bangumibuddy.pojo.anime.Anime;
import org.bangumibuddy.pojo.anime.AnimeMetaData;
import org.bangumibuddy.pojo.anime.AnimeSummary;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class DefaultRssParser implements RSSParser {

    protected SyndFeed feed;

    List<TorrentFile> torrentFiles = null;

    TorrentParser torrentParser = new DefaultTorrentParser();

    String rssUrl = null;
    protected AnimeMetaParser animeMetaParser = AnimeMetaParserFactory.create();

    public DefaultRssParser() {
        feed = null;
    }

    @SneakyThrows
    public DefaultRssParser(@NotNull File rssfile) {
        if (rssfile == null) {
            throw new RuntimeException("rss file is null");
        }
        InputStream inputStream = new FileInputStream(rssfile);
        Init(inputStream);
    }

    @SneakyThrows
    public DefaultRssParser(@NotNull String url) {
        if (url == null) {
            throw new RuntimeException("rss url is null");
        }

        URL rssUrl = new URL(url);
        InputStream inputStream = rssUrl.openStream();
        Init(inputStream);
        this.rssUrl = url;
    }

    @SneakyThrows
    private void Init(InputStream inputStream) {
        XmlReader reader = new XmlReader(inputStream);
        feed = new SyndFeedInput().build(reader);
    }


    @Override
    public String getTitle() {
        return feed.getTitle();
    }

    @Override
    public String getPoster() {
        return "";
    }

    @Override
    public String getSeason() {
        return "";
    }

    @Override
    public Integer getSeasonNumber() {
        //试图从种子文件名中提取季数
        List<TorrentFile> getTorrentFiles = getTorrentFiles();
        for (TorrentFile torrentFile : getTorrentFiles) {
            String name = torrentFile.getTorrentName();
            AnimeMetaData parse = animeMetaParser.parse(name);
            return parse.getSeason();
        }

        return null;
    }

    @Override
    public List<String> getTorrentURLs() {
        if(torrentFiles == null)
            getTorrentFiles();

        List<String> torrentURLs = new ArrayList<>();
        for (TorrentFile torrentFile : torrentFiles) {
            torrentURLs.add(torrentFile.getUrl());
        }

        return torrentURLs;
    }

    private List<String> InternalGetTorrentURLs() {
        List<SyndEntry> entries = feed.getEntries();
        List<String> torrentURLs = new ArrayList<>();
        for (SyndEntry entry : entries) {
            SyndEnclosure enclosure = (SyndEnclosure) entry.getEnclosures().get(0);
            torrentURLs.add(enclosure.getUrl());
        }

        return torrentURLs;
    }

    @Override
    public String getYear() {
        return "";
    }

    @Override
    public List<TorrentFile> getTorrentFiles() {
        if (torrentFiles == null)
            torrentFiles = torrentParser.getTorrentFilesByEntries(getEntries());

        return torrentFiles;
    }

    @Override
    public String getUrl() {
        if(rssUrl == null)
            throw new RuntimeException("请使用URL形式来初始化RSSParser");

        return rssUrl;
    }

    @Override
    public String getRssName() {
        List<SyndEntry> entries = feed.getEntries();
        for (SyndEntry entry : entries) {
            AnimeMetaData parse = animeMetaParser.parse(entry.getTitle());
            if(parse != null)
                return getTitle() + getSeason() + "[" +parse.getGroup()+ "]";
        }

        return getTitle() ;
    }

    @Override
    public Anime getAnime() {
        Anime anime = new Anime();
        anime.setName(getTitle());
        anime.setRssUrl(getUrl());
        anime.setSeason(getSeason());
        anime.setSeasonNumber(getSeasonNumber());
        anime.setTorrentFiles(getTorrentFiles());

        AnimeSummary summary= new AnimeSummary();
        summary.setName(getTitle());
        summary.setPoster(getPoster());
        summary.setSeason(getSeason());
        anime.setSummary(summary);

        return anime;
    }

    @Override
    public List<SyndEntry> getEntries() {
        return feed.getEntries();
    }
}
